package com.main;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import com.core.EmployeeManager;
import com.core.SearchEmployee;
import com.objects.Employee;


public class MainProgram {
	public static void main(String[] args) {
		StartConsole();
	}
	private static boolean isNumeric(Object intString){
		try{
			Integer.parseInt(intString.toString());
			return true;
		}catch(NumberFormatException ex){
			return false;
		}
	}
	private static void StartConsole() {
		try{
			BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
			String reader ="";

			while(!reader.equalsIgnoreCase("4")){

				System.out.println("[1]Search Employee \r\n[2]Update Employee Seat\r\n[3]Delete Employee Seat\r\n[4] EXIT");
				reader = bufferRead.readLine();

				if (reader.equalsIgnoreCase("1")){
					System.out.println("Please enter the Employee ID");
					String employeeID= bufferRead.readLine();

					//check if the input is a valid numeric char
					while(!(isNumeric(employeeID))){
						System.out.println("Enter the Employee ID");
						employeeID=bufferRead.readLine();
					}

					//this will display all the fields from the search
					if (SearchEmployee.searchEmployeeSeatByID(Integer.parseInt(employeeID))){
						SearchEmployee.displayEmployeeSeatMap();
					}

					//give the option to print the existing display
					System.out.println("Print the result [Y] [N]");

					String export = bufferRead.readLine();

					if(export.equalsIgnoreCase("y")){
						//export to text file
						if (SearchEmployee.exportEmployeaSeatMapToText()){
							System.out.println("***Success***");	
						}else{
							System.out.println("Export file fail");
						}
					}

				}else if (reader.equalsIgnoreCase("2")){
					boolean isValidID=false;
					while (isValidID==false){

						System.out.println("Update Seat assignment");
						System.out.println("Enter ID of the Employee:");
						String employeeID = bufferRead.readLine();
						//display details of the employee
						if (SearchEmployee.searchEmployeeSeatByID(Integer.parseInt(employeeID))){
							//call update employee passing the parameters
							isValidID=true;
							System.out.println("Enter the New Seat Code");
							String newSeatCode=bufferRead.readLine();
							//assign the new seatcode
							//will add checking if the seat is assigned or available and existing
							EmployeeManager em = new EmployeeManager();
							if (em.updateEmployeeSeat(employeeID, newSeatCode)){
								System.out.println("Successfully Updated!");
							}else{
								System.out.println("Error updating");
							}
							//display confirmation that the seat is updated

						}else{
							isValidID=false;
							System.out.println("Employee ID not found!");
						}
					}
				}else if (reader.equalsIgnoreCase("3")){
					System.out.println("Delet employee seat assignment.");
					System.out.println("Enter the Employee ID:");
					String employeeID = bufferRead.readLine();
					while(!(isNumeric(employeeID))){
						System.out.println("Enter a valid numeric Employee ID");
						employeeID=bufferRead.readLine();
						EmployeeManager em = new EmployeeManager();
						if (SearchEmployee.searchEmployeeSeatByID(Integer.parseInt((employeeID)))){

							System.out.println("Enter the Seat Code:");
							String seatCode = bufferRead.readLine();

							System.out.println("Are you sure you want to delete seat assignment for " + employeeID +"? [y][n]" );
							String sureDelete =bufferRead.readLine();
							if(sureDelete.equalsIgnoreCase("y")){
								//if employee is found delete his current seat assignment
								Employee employee= new Employee();
								employee=em.searchEmployee(employeeID);

								if (em.deleteEmployeeSeat(employee, seatCode)){
									System.out.println("Seat Assignment for " + employeeID +"deleted." );			
								}

							}else if(sureDelete.equalsIgnoreCase("n")){
								System.out.println("Thank you.");
							}

						}else{
							System.out.println("No Employee found");
						}
					}
				}
			}

			bufferRead.close();

		}catch(Exception ex){
			System.out.println(ex);
		}

	}
}
